package cn.cdutetc.foxwatch.serveice.imp.message;

import cn.cdutetc.foxwatch.common.ExceptionEnum;
import cn.cdutetc.foxwatch.config.JedisConnectionFactory;
import cn.cdutetc.foxwatch.exception.MyException;
import cn.cdutetc.foxwatch.mapper.message.MailMapper;
import cn.cdutetc.foxwatch.pojo.message.MessagePojo;
import cn.cdutetc.foxwatch.serveice.message.MailService;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.micrometer.common.util.StringUtils;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.io.UnsupportedEncodingException;

/**
 * @author 王勇
 * @ClassName： MaillServiceImpl
 * @ClassPath： cn.cdutetc.foxwatch.serveice.imp.message.MaillServiceImpl
 * @Description：
 * @date 2024/10/5 12:34
 */
@Service
public class MailServiceImpl extends ServiceImpl<MailMapper, MessagePojo> implements MailService {
    @Value("${spring.mail.username}")
    private String sender;
    @Autowired
    private JavaMailSender javaMailSender;

    public void test() throws MessagingException, UnsupportedEncodingException {
        {
            //邮件主题
            String subject = "测试邮件主题";
            //接受者
            String recipient = "2157452092@qq.com";
            //正文
            String text = "这是测试邮箱";
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            // 设置发件人名称和邮箱地址
            System.out.println(sender);
            InternetAddress fromAddress = new InternetAddress(sender, "狐狸手环");
            helper.setFrom(fromAddress);
            helper.setTo(recipient);
            helper.setSubject(subject);
            helper.setText(text);
            javaMailSender.send(mimeMessage);
            System.out.println("发送成功");
        }
    }

    @Override
    public void sendMail(MessagePojo messagePojo) throws MyException {
        //邮件主题
        String subject = messagePojo.getSubject();
        //接受者
        String recipient = messagePojo.getRecipient();
        //正文
        String text = messagePojo.getText();
        System.out.println(recipient + ":" + text + ":" + subject);
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            // 设置发件人名称和邮箱地址
            System.out.println(sender);
            InternetAddress fromAddress = new InternetAddress(sender, "狐狸手环");
            helper.setFrom(fromAddress);
            helper.setTo(recipient);
            helper.setSubject(subject);
            helper.setText(text);
            javaMailSender.send(mimeMessage);
            System.out.println("发送成功");
        } catch (Exception e) {
            throw new MyException(200, ExceptionEnum.EmailError.getDesc());
        }
    }

    @Override
    public void sendCode(String recipientObj) throws MyException {
        System.out.println(recipientObj);
        if (StringUtils.isEmpty(recipientObj)) {
            throw new MyException(200, ExceptionEnum.ParameterError.getDesc());
        }
        String recipient = JSON.parseObject(recipientObj).getString("recipient");

        int code = RandomUtil.randomInt(1111, 9999);
        MessagePojo messagePojo = MessagePojo.builder().recipient(recipient).text("【狐狸手环】您的验证码为：" + code + "，如非本人操作，请忽略").subject("狐狸手环验证码").build();
        this.sendMail(messagePojo);
        Jedis jedis = JedisConnectionFactory.getJedis();
        try {
            jedis.hset(recipient, "code", String.valueOf(code));
            jedis.expire(recipient, 60);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }


    }


}
